package com.portal.framework.exception.handler;

import java.text.MessageFormat;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.portal.framework.exception.ExceptionHandleException;

/**
 * 把特定的Exception变成一个特定信息(message)的异常
 * 
 * @author 卢烁波
 * @date 2007-3-10
 */
public class MsgExceptionHandler extends AbstractExceptionHandler {
	private static final Log log = LogFactory.getLog(MsgExceptionHandler.class);

	private String msg;

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public MsgExceptionHandler(String msg) {
		this.msg = msg;
	}

	public MsgExceptionHandler(Class<? extends Throwable> exceptionClass,
			String msg) {
		this.msg = msg;
		this.exceptionClass = exceptionClass;
	}

	public void handle(Throwable e) throws Throwable {
		if (log.isDebugEnabled()) {
			String exceptionMsg = "进行异常处理...异常类为[{0}],异常处理类为[{1}],异常信息为[{2}]";
			log.debug(MessageFormat.format(exceptionMsg,
					e.getClass().getName(), this.getClass().getName(), msg));
		}
		e.printStackTrace();
		throw new ExceptionHandleException(msg, super.getStackTraceInfo(e));
	}

	@Override
	public String toString() {
		return MessageFormat.format("信息异常类处理.信息=[{0}],处理的异常=[{1}]", msg,
				this.exceptionClass.getName());
	}

}
